const {
  MemberModel
} = require('../../../utils/member-model.js');
var utils =
  require("../../../utils/util.js");
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lotteryTotal: 0, //飞豆
    lotteryData: [], //门店列表飞豆统计
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo(); //微信会员信息
    this.getUserAssetDetail(); //会员资产详情
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      that.setData({
        memberName: data.nickname || "", //会员名称
        src: data.avatarUrl || "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 会员资产详情
   */
  getUserAssetDetail() {
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 1; //飞豆
    param.assetStatus = 0; //0:正常 3:过期
    memberModel.userAssetDetail(param).then(data => {
      this.setData({
        lotteryTotal: utils.formatCurrency(data.assetAvail) || 0,
      })
      var that = this;
      var dataList = [];
      if (data && data.assetShopList && data.assetShopList.length > 0) {
        for (var i = 0; i < data.assetShopList.length; i++) {
          var item = data.assetShopList[i];
          var list = {};
          list.partnerName = item.partnerName;
          list.shopId = item.shopId;
          list.shopName = item.shopName;
          list.assetAvail = utils.formatCurrency(item.assetAvail);
          dataList.push(list);
        }
        this.setData({
          lotteryData: dataList
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  goLotteryDetail(event) {
    var shopId = event.currentTarget.dataset.id; //页面类型
    var cardNo = event.currentTarget.dataset.code; //卡号
    wx.navigateTo({
      url: '/pages/memberCenter/lotteryDetail/index?shopId=' + shopId + '&cardNo=' + cardNo
    })
  }

})